import axios from "axios";

axios.interceptors.response.use(
    response => {
        return response;
    },
    error => {
        console.log(error, ">>>>>异常信息");
        if (error.response) {
            switch (error.response.status) {
                case 30001:
                    // 返回 30001 清除 token 信息并跳转到登录页面
                    localStorage.removeItem("TOKEN");
                    window.location.href = "/login";
                    break;
                case 30002:
                    // 返回 30002 清除 token 信息并跳转到登录页面
                    localStorage.removeItem("TOKEN");
                    window.location.href = "/login";
                    break;
            }
        }
        return Promise.reject(error.response)
        // 返回接口返回的错误信息
    });
// ajax请求统一增加请求头

//创建一个axios的对象
const API = {
        baseURL: "http://localhost:9981/",
        plURL: "http://localhost:9983/",
        park_url: "/park",
        timeout: 5000,
        init() {
            axios.defaults.headers.common['token'] = localStorage.getItem("TOKEN");
        },
        getParksBylimit(param, callBack) {
            this.init();
            var addressParam = "";
            if (param.address !== "") {
                addressParam = "?address=" + param.address;
            }
            axios.get(
                this.baseURL +
                "station" +
                this.park_url +
                "/" +
                param.cityCode +
                "/" +
                param.page +
                "/" +
                param.limit +
                addressParam
            )
                .then((res) => {
                    callBack(res.data);
                });
        }
        ,
        getParkCount(param, callBack) {
            this.init();
            var addressParam = "";
            if (param.address !== "") {
                addressParam = "?address=" + param.address;
            }
            axios
                .get(this.baseURL + "station/park/" + param.cityCode + addressParam)
                .then((res) => {
                    callBack(res.data);
                });
        }
        ,
        addBike(data, callBack) {
            this.init();
            if (data.platform === "轻悠") {
                data.pic =
                    data.bikeMode === "1"
                        ? "https://i03.c.aliimg.com/img/ibank/2014/609/150/1853051906_2071011329.jpg"
                        : "https://upload.ikanchai.com/2017/0506/1494034024215.jpg";
            }
            if (data.platform === "永安行") {
                data.pic =
                    data.bikeMode === "1"
                        ? "https://www.ibike668.com/images/platform/nav-ebike.png"
                        : "https://www.ibike668.com/images/platform/nav-bicycle.png";
            }

            axios
                .request({
                    url: this.baseURL + "bike" + "/add",
                    data: data,
                    method: "post",
                })
                .then((res) => {
                    callBack(res.data);
                });
        }
        ,
        findBikeByParkName(param, callback) {
            this.init();
            axios.get(this.baseURL + "station" + "/bike/" + param).then((res) => {
                callback(res.data);
            });
        }
        ,
        delBike(param, callback) {
            this.init();
            axios.get(this.baseURL + "bike" + "/del/" + param.bikeId).then((res) => {
                callback(res.data);
            });
        }
        ,
        queryBikeByDetail(param, callback) {
            this.init();
            var reqData = "";
            reqData += "?parkName=" + param.parkName;
            reqData += "&info=" + param.info;
            if (param.platform !== "") {
                reqData += "&platform=" + param.platform;
            }
            if (param.startMileage !== "") {
                reqData += "&startMileage=" + param.startMileage;
            }
            if (param.endMileage !== "") {
                reqData += "&endMileage=" + param.endMileage;
            }
            console.log(reqData);
            axios.get(this.baseURL + "bike" + "/query" + reqData).then((res) => {
                callback(res.data);
            });
        }
        ,

//获取武进区行政区边界
        getWujinBoundary(callback) {
            this.init();
            axios.get(this.baseURL + "boundary/320412").then((res) => {
                callback(res);
            });
        }
        ,

//修改停放点的位置
        updateParkPoint(data, callback) {
            this.init();
            axios
                .request({
                    url: this.baseURL + "station" + "/park/update",
                    method: "post",
                    data: JSON.stringify(data),
                })
                .then((res) => {
                    callback(res.data);
                });
        }
        ,
        async createPark(data, callback) {
            this.init();
            let parkRES = await this.addPark(data);//先创建节点
            if (parkRES.code === 200) {
                let relRES = await this.addRelation(data);//再添加关系
                if (relRES.code === 200) {
                    callback(relRES.data);
                }
            }
        }
        ,
//添加单车停放点
        async addPark(data) {
            this.init();
            axios
                .request({
                    url: this.baseURL + "station" + "/park/add",
                    data: JSON.stringify(data),
                    method: "post",
                })
                .then((res) => {
                    return res.data;
                });
        }
        ,
//创建关系
        async addRelation(data) {
            this.init();
            axios
                .request({
                    url: this.baseURL + "station" + "/park/relationship",
                    data: JSON.stringify(data),
                    method: "post",
                })
                .then((res) => {
                    return res.data;
                });
        }
        ,
//获取范围内的单车停放点数据
        getBikeParks(data, callback) {
            this.init();
            axios
                .request({
                    url: this.baseURL + "station" + "/park",
                    data: JSON.stringify(data),
                    method: "post",
                    headers: {
                        "Content-Type": "application/json;charset=UTF-8",
                        token: localStorage.getItem("TOKEN")
                    },
                })
                .then((res) => {
                    callback(res);
                });
        }
        ,
//获取范围内的公交车信息
        getBusSite(data, callback) {
            this.init();
            axios
                .request({
                    url: this.baseURL + "station" + "/bussite",
                    data: data,
                    method: "post",
                })
                .then((res) => {
                    callback(res);
                });
        }
        ,
//获取范围内的地铁数据
        getSubway(data, callback) {
            this.init();
            axios
                .request({
                    url: this.baseURL + "station" + "/subway",
                    data: data,
                    method: "post",

                })
                .then((res) => {
                    callback(res);
                });
        }
        ,

//获取电子围栏信息
        getParkRail(param, callback) {
            this.init();
            axios.get(this.baseURL + "rail" + "/query/" + param).then((res) => {
                callback(res.data);
            });
        }
        ,
// 获取共享单车停放点的共享单车信息
        getBikeByParkName(param, callback) {
            this.init();
            axios.get(this.baseURL + "bike" + "/query?parkName=" + param.parkName).then((res) => {
                callback(res.data);
            });
        },
        getBikePlatformCount(callback) {
            this.init();
            axios.get(this.baseURL + "bike" + "/query/platform").then((res) => {
                callback(res.data);
            });
        },
        getPlatformInfo(param, callback) {
            this.init();
            axios.get(this.plURL + "platform" + "/query?name=" + param.name).then((res) => {
                callback(res.data);
            });
        },
        getPlatformInfoByUserId(param, callback){
            this.init();
            axios.get(this.plURL + "platform" + "/query/byUser?userId=" + param.userId).then((res) => {
                callback(res.data);
            });
        }
    }
;
export default API;
